<template>
  <el-dialog :title="formData.id?'编辑部门':'新增部门'" :visible="isShowDialog" @close="btnCancel">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form ref="addForm" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" style="width:80%" placeholder="1-15个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" style="width:80%" placeholder="1-15个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="formData.manager" style="width:80%" placeholder="请选择">
          <el-option v-for="user in employeeList" :key="user.id" :value="user.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getEmployeSimple } from '@/api/employees'
import { addDepts, getDeptsDetailById, editDepts } from '@/api/departments'
export default {
  props: {
    isShowDialog: {
      type: Boolean,
      //   required: true,
      default: false
    },
    nodeData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      formData: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '该项不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '1-15位之间', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '该项不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '1-15位之间', trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '该项不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '1-300位之间', trigger: 'blur' }
        ]
      },
      employeeList: []
    }
  },
  async created() {
    const res = await getEmployeSimple()
    // console.log(70, res)
    this.employeeList = res
  },
  methods: {
    async btnOk() {
      // 校验表单
      await this.$refs.addForm.validate()
      // 发送请求 根据当前 formData是否具有id决定修改还是新增
      if (this.formData.id) {
        // 编辑
        await editDepts(this.formData)
        // 提醒用户
        this.$message.success('修改成功')
      } else {
        await addDepts({
          ...this.formData,
          pid: this.nodeData.id
        })
        // 提醒用户
        this.$message.success('添加成功')
      }

      // 更新页面
      this.$emit('reloadPage')
      // 关闭弹窗
      this.$emit('closeDialog')
    },
    btnCancel() {
      // 关闭弹窗(通知父组件)
      this.$emit('closeDialog')
      // 手动清理数据
      this.formData = {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      }
      // 清理校验错误
      this.$refs.addForm.resetFields()
    },
    async getDeptsDetail(id) {
      // 这是回显数据函数
      // 如果你给我一个部门id
      // 我就发请求，取回部门详情，填充到formData
      this.formData = await getDeptsDetailById(id)
      console.log(this.formData)
    }
  }
}
</script>

<style>

</style>
